﻿using System;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            GuessNumber(1, 100);
            Console.ReadLine();
        }

        static void GuessNumber(int begin, int end)
        {
            int target = new Random().Next(begin, end + 1);
            Console.WriteLine("请猜测一个{0}到{1}之间的数字：", begin, end);
            GuessNumber(begin, end, target);
        }

        static void GuessNumber2(int begin, int end, int target)
        {
            int guess = 0;
            bool success = false;
            if (int.TryParse(Console.ReadLine(), out guess))
            {
                if (guess < begin || guess > end)
                {
                    Console.WriteLine("输入的数字超过范围，请输入一个{0}到{1}之间的数字！", begin, end);
                }
                else if (guess == target)
                {
                    Console.WriteLine("恭喜你，猜对了！");
                    success = true;
                }
                else if (guess < target)
                {
                    Console.WriteLine("猜小了，请再猜！");
                }
                else
                {
                    Console.WriteLine("猜大了，请再猜！");
                }
            }
            else
            {
                Console.WriteLine("输入的内容无效，请输入一个{0}到{1}之间的数字！", begin, end);
            }
            if (!success)
            {
                GuessNumber(begin, end, target);
            }
        }

        static void GuessNumber(int begin, int end, int target)
        {
            int guess = 0;
            do
            {
                if (int.TryParse(Console.ReadLine(), out guess))
                {
                    if (guess < begin || guess > end)
                    {
                        Console.WriteLine("输入的数字超过范围，请输入一个{0}到{1}之间的数字！", begin, end);
                    }
                    else if (guess == target)
                    {
                        Console.WriteLine("恭喜你，猜对了！");
                        //break;
                    }
                    else if (guess < target)
                    {
                        Console.WriteLine("猜小了，请再猜！");
                    }
                    else
                    {
                        Console.WriteLine("猜大了，请再猜！");
                    }
                }
                else
                {
                    Console.WriteLine("输入的内容无效，请输入一个{0}到{1}之间的数字！", begin, end);
                }
            }
            while (guess != target);

        }
    }
}
